<?php

namespace App\Http\Controllers\Admin;

use App\Models\Project_admin_user;
use Illuminate\Http\Request;
use DB;
use Hash;
use Session;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
   /**
   *    后台登陆页面
   */
   public function loginPage()
   {
       return view('admin.login');
   }

   /**
   *    登陆方法                                   /*有空仔细看看
   */
   public function doLogin(Request $request)
   {
       $res = Project_admin_user::existUserName($request);
       if(!$res){
           $this->data = $this->makeJsCode(1,"用户名不存在");
           return back()->with($this->data);
       } elseif ($res->password != $request->password){
           $this->data = $this->makeJsCode(1,"密码不正确");
           return back()->with($this->data);
       }

       session()->set("adminuser",$res); //存储session跳转页面

//       $allPer = DB::select("select nr.username,nr.mode,nr.path from project_admin_user_role ur,project_admin_role_node rr,project_admin_node nr where ur.rid=rr.rid and rr.nid=nr.id and ur.uid=:id",['id'=>$res->id]);
       //获取当前登陆者的权限 /*
       $allPer = Project_admin_user::permission($res);

       if($allPer){
           return redirect(route('admin.welcome'));
       }
       $this->data = $this->makeJsCode(1,"用户名或密码错误");
       return back() ->with($this->data);
   }

   /**
   *  退出登录
   */
   public function loginOut()
   {
       session(['uid'=>null]);
      return redirect('/admin/login');
   }

}
